package com.foreknow.view;
import com.foreknow.model.Admin;
import com.foreknow.service.AdminService;
import com.foreknow.service.impl.AdminServiceImpl;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Login extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Login() {
        setBackground(SystemColor.activeCaption);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setForeground(SystemColor.activeCaption);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblUsename = new JLabel("usename");
        lblUsename.setFont(new Font("宋体", Font.PLAIN, 15));
        lblUsename.setBounds(73, 52, 105, 15);
        contentPane.add(lblUsename);

        JLabel lblPassword = new JLabel("password");
        lblPassword.setFont(new Font("宋体", Font.PLAIN, 15));
        lblPassword.setBounds(72, 121, 106, 15);
        contentPane.add(lblPassword);

        textField = new JTextField();
        textField.setBounds(155, 49, 206, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(155, 118, 206, 21);
        contentPane.add(passwordField);
        passwordField.setColumns(10);

        JButton btnNewButton = new JButton("\u767B\u5F55");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //登录的操作
                AdminService service = new AdminServiceImpl();
                //获取用户输入的内容（用户名和密码）
                String name = textField.getText();
                String pass = passwordField.getText();
                //调用Service中的login方法
                Admin admin = service.login(name,pass);
                if(admin != null) {
                    System.out.println("login success......");
                    //跳转到主页面
                    MainWindow mainWindow = new MainWindow();
                    //隐藏Login登录容器
                    Login.this.dispose();
                }else {
                    System.out.println("login error......");
                }
            }
        });
        btnNewButton.setBounds(85, 200, 93, 23);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("\u53D6\u6D88");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //取消的操作
            }
        });
        btnNewButton_1.setBounds(257, 200, 93, 23);
        contentPane.add(btnNewButton_1);
    }
}

